import { ref } from 'vue';
import { useStorage } from '@/utils/index';

export default () => {
	const storage=useStorage()
	const USERINFO = ref()
	const setUserInfo = (user) => {
		
		USERINFO.value = user
		storage.set('USERINFO', user);
	}

	const getToken = () => {
		return USERINFO.value?.token;
	}

	const clearUserInfo = () => {
		USERINFO.value = undefined
		return storage.remove('USERINFO')
	}
	const initUserInfo = () => {
		const user = storage.get('USERINFO')
		if (user !== null) {
			setUserInfo(user);
		}
	}
	const isLogin = () => {
		return USERINFO.value !== undefined && USERINFO.value.token !== undefined && USERINFO.value.token !== null && USERINFO.value.token !== '';
	}
	return {
		USERINFO,
		setUserInfo,
		getToken,
		clearUserInfo,
		initUserInfo,
		isLogin
	}
}
